const puppeteer = require('puppeteer')
const  url = `http://zhongguose.com/`

//睡眠函数
const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time)
  })

;(async ()=>{
    console.log('start visit the target page')

     const browser = await puppeteer.launch({
        args: ['--no-sandbox']
     })

    const page = await browser.newPage()
    await page.goto(url, {
            waitUntil: 'networkidle2'
            })

    await sleep(3000) 
    const result = await page.evaluate(()=>{
        let items = document.querySelectorAll("#colors li div a")
        let links = []
        if(items.length >=1){
            items.forEach((item)=>{
                let rgb = item.parentNode.parentNode.style.borderTop.substr(10)
                let arr = item.children
                let name = arr[0].innerHTML
                let pinyin = arr[1].innerHTML
                let cx16 ="#"+ arr[2].innerHTML
                let moreColor =  ['紫', '红', '灰','白', '蓝', '绿', '黄']
                let c_type = name[name.length-1]
                if(moreColor.indexOf(c_type) == -1){
                    c_type = "其他"
                }
                links.push({
                    name,
                    pinyin,
                    cx16,
                    rgb,
                    c_type
                })
            })
        }
        return links
    }).catch(err=>{
        console.log(2,err)
    })

    await browser.close()

    process.send({result})
    process.exit(0)
})()